package configuration;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.*;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


public class Configuration {
	private String file;
	private Document domDoc;
	private ConcurrentHashMap <String, ModuleEntry>db;
	public Configuration(String fn) {
		db = new ConcurrentHashMap<String,ModuleEntry>();
		try{
			file = fn;
			DocumentBuilderFactory theFac = DocumentBuilderFactory.newInstance();
			DocumentBuilder docComp = theFac.newDocumentBuilder();
			domDoc = docComp.parse(new File(fn));
			domDoc.getDocumentElement ().normalize ();

			NodeList temp = domDoc.getChildNodes();
			if(temp.item(0).getNodeName().equals("AIMCONFIG")){


				temp = domDoc.getElementsByTagName("Module");
				Element mods;
				NodeList temp1;
				NodeList temp0;
				Element idents;
				NodeList tagEl;
				NodeList valueEl;
				String tag = "";
				NodeList valueL;
				String value = "";
				String nm;
				ModuleEntry tempMod;
				DBEntry tempEntry;
				for(int i = 0; i < temp.getLength(); i++) {
					mods = (Element) temp.item(i);
					temp0 = mods.getChildNodes();
					nm =  temp0.item(0).getNodeValue().trim();
					if(db.containsKey(nm)) {
						System.err.println("Module Already Exists");
					}
					else {
						tempMod = new ModuleEntry(nm);

						temp1 = mods.getElementsByTagName("Identifier");
						for(int j=0; j< temp1.getLength(); j++) {
							idents = (Element) temp1.item(j);
							String identifier = idents.getChildNodes().item(0).getNodeValue().trim();
							tempEntry = new DBEntry(identifier);
							tagEl =idents.getElementsByTagName("Tag");
							if(tagEl == null) {
								//We Need a bad file exception
							}
							else {
								for(int k = 0; k<tagEl.getLength(); k++) {
									idents = (Element) tagEl.item(k);
									tag = idents.getChildNodes().item(0).getNodeValue();
									valueL = idents.getElementsByTagName("Value");
									Element valueLE = (Element) valueL.item(0);
									value = valueLE.getChildNodes().item(0).getNodeValue().trim();						
										try {
											tempEntry.add(tag, value);
										}
										catch(EntryExists e) {
											System.err.println("Already in DB");
										}
									try {
										tempMod.add(tempEntry);
									}
									catch(EntryExists e) {
										//System.err.println("Already in DB");
									}
									}
									}
									}

							db.put(nm, tempMod);
									}
								}
							}
		}


		catch(ParserConfigurationException e) {

		}
		catch(IOException e) {
			System.out.println("FileNotFound");
		}
		catch(SAXException e) {

		}
	}
	public boolean isEntry(String mod, String id) {
		if(db.containsKey(mod)){
			ModuleEntry temp = db.get(mod);
			return temp.isEntry(id);
		}
		else {
			return false;
		}
	}
	public boolean isEntry(String mod, String id, String tag) {
		if(db.containsKey(mod)){
			ModuleEntry temp = db.get(mod);
			return temp.isEntry(id, tag);
		}
		else {
			return false;
		}
	}
public void addEntry(String module, String id, String tag, String value) throws EntryExists {
	if(db.containsKey(module))
	{
		db.get(module).addLeaf(id, tag, value);

	}
	else {
		ModuleEntry temp = new ModuleEntry(module);
		DBEntry tempa = new DBEntry(id);
		tempa.add(tag, value);
		temp.add(tempa);
		db.put(module, temp);
	}
}
}

